<template>
  <div class="container">
    <el-card class="articles">
      <el-row>
        <el-col :span="18">
          <el-form label-width="80px" size="small" :inline="true">
            <el-form-item label="关键字">
              <el-input v-model="page.keyword" placeholder="根据文章标题搜索"></el-input>
            </el-form-item>
            <el-form-item label="状态">
              <el-select v-model="page.state" placeholder="请选择">
                <el-option :value="1" label="启用"></el-option>
                <el-option :value="0" label="禁用"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button @click="clear">清除</el-button>
              <el-button @click="filter" type="primary">搜索</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <!-- 控制按钮右移 -->
        <el-col :span="6" style="text-align: right">
          <el-button type="success" icon="el-icon-edit" size="small" @click="openDialog">新增技巧</el-button>
        </el-col>
      </el-row>
      <!-- 分割条 -->
      <el-row>
        <el-alert :title="`数据一共 ${total} 条`" type="info" show-icon :closable="false"></el-alert>
      </el-row>
      <!-- 表格 -->
      <el-table :data="articlesList" :header-cell-style="{ background: '#fafafa' }" style="margin-top: 20px">
        <el-table-column label="序号" type="index" width="80px"></el-table-column>
        <el-table-column label="文章标题" prop="title" width="400px">
          <template v-slot="{ row }">
            {{ row.title }}
            <el-button v-show="row.videoURL !== null" type="text" icon="el-icon-film"></el-button>
          </template>
        </el-table-column>
        <el-table-column label="阅读数" prop="visits"></el-table-column>
        <el-table-column label="录入人" prop="username"> </el-table-column>
        <el-table-column label="录入时间" prop="createTime">
          <template slot-scope="{ row }">
            {{ row.createTime | parseTimeByString }}
          </template>
        </el-table-column>
        <el-table-column label="状态" prop="state" width="100px">
          <template slot-scope="{ row }">
            {{ row.state == "1" ? "已启用" : "已禁用" }}
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" align="center" width="180px">
          <template slot-scope="{ row }">
            <el-button type="text" @click="previewArticle(row)">预览</el-button>
            <el-button type="text" @click="toggleState(row)">
              {{ row.state === 1 ? "禁用" : "启用" }}
            </el-button>
            <el-button type="text" @click="openDialog(row)" :disabled="row.state === 1">修改</el-button>
            <el-button type="text" @click="delArticle(row)" :disabled="row.state === 1">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination background style="margin-top: 20px; text-align: right" @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page.page" :page-sizes="[5, 10, 20, 50]" :page-size="page.pagesize" layout=" prev, pager, next,sizes, jumper" :total="total">
      </el-pagination>
    </el-card>
    <!-- 预览 -->
    <articles-preview :showPreDialog.sync="showPreDialog" :currArticle="currArticle"></articles-preview>
    <!-- 添加/修改 -->
    <articles-add :showDialog.sync="showDialog" :currArticle="currArticle" @updateList="updateList"></articles-add>
  </div>
</template>

<script>
import { list, changeState, remove } from '@/api/hmmm/articles'
import articlesPreview from '../components/articles-preview.vue'
import articlesAdd from '../components/articles-add.vue'
export default {
  components: { articlesPreview, articlesAdd },
  data() {
    return {
      articlesList: [],
      currArticle: {},
      total: 0,
      page: {
        page: 1,
        pagesize: 10,
        keyword: null,
        state: null
      },
      showDialog: false, //新增、修改
      showPreDialog: false //预览
    }
  },
  created() {
    this.getArticlesList()
  },
  methods: {
    async getArticlesList() {
      const { data } = await list(this.page)
      // console.log(data);
      this.total = data.counts
      this.articlesList = data.items
    },
    handleSizeChange(val) {
      this.page.pagesize = val
      this.getArticlesList()
    },
    handleCurrentChange(val) {
      this.page.page = val
      this.getArticlesList()
    },
    clear() {
      this.page = {
        page: 1,
        pagesize: 10,
        keyword: null,
        state: null
      }
      this.getArticlesList()
    },
    filter() {
      this.page.page = 1
      this.getArticlesList()
    },
    async toggleState(row) {
      await changeState({
        id: row.id,
        state: row.state === 1 ? 0 : 1
      })
      this.$message.success('操作成功')
      this.getArticlesList()
    },
    //预览
    previewArticle(row) {
      this.currArticle = row
      this.showPreDialog = true
    },
    //修改
    openDialog(row) {
      this.currArticle = row.id ? row : {}
      this.showDialog = true
    },
    updateList() {
      if (!this.currArticle.id) this.page.page = 1
      this.getArticlesList()
    },
    //删除
    async delArticle(row) {
      await this.$confirm('此操作将永久删除该文章, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
      await remove(row)
      this.$message.success('删除成功')
      this.getArticlesList()
    }
  }
}
</script>

<style scoped lang='scss'>
.articles {
  margin: 10px;
}
</style>
